package jeconbond.experiment.computation;

import laboratoryQ.flatness.DiagramData;
import laboratoryQ.flatness.Point;

import java.util.List;
import java.util.Arrays;

import jeconbond.experiment.BaseJEBExperimentSettings;

public class DiagramUtils {
////	public static final int[][] STYLES_COLLECTION = new int[][] {
////		new int[] {40, 7},
////		new int[] {5, 5},
////		new int[] {40, 7, 5, 5},
////	};
//	public static final int[][] STYLES_COLLECTION = new int[][] {
//		new int[] {120, 30},
//		new int[] {15, 23},
//		new int[] {120, 30, 15, 23},
//	};

	public static final String[] LABELS_Y_PVA = {
		"Price",
		"Value",
		"Activity",
	};

	public static final String[] LABELS_TIME_3 = new String[3];
	static {
		Arrays.fill(LABELS_TIME_3, "Time");
	}

	public static double[] spyTrack2diagram(List<Double> track) {
		int n = track.size();
		double[] result = new double[n];
		for (int i = 0; i < n; i++) {
			result[i] = track.get(i);
		}
		replace_NAN_INF(result);
		return result;
	}

	public static void replace_NAN_INF(double[] arr) {
		replace_NAN_INF(arr, Double.NaN);
	}

	public static void replace_NAN_INF(double[] arr, double defaultVal) {
		int n = arr.length;
		double prev = defaultVal;
		for (int i = 0; i < n; i++) {
			if (!Double.isNaN(arr[i]) && !Double.isInfinite(arr[i])) {
				prev = arr[i];
				break;
			}
		}
		for (int i = 0; i < n; i++) {
			if (Double.isNaN(arr[i]) || Double.isInfinite(arr[i])) {
				arr[i] = prev;
			}
			prev = arr[i];
		}
	}

//	public static Map<IResourceType, Color> res2ColorMap(
//			Color[] colors,
//			IExperimentSettings settings
//	) {
//		Map<IResourceType, Color> map = new HashMap<IResourceType, Color>();
//		IResourceType[] resources = settings.getResources();
//		int n = resources.length;
//		for (int i = 0; i < n; i++) {
//			map.put(resources[i], colors[i]);
//		}
//		return map;
//	}
//
//	public static IDrawStrategy[] styleArrayCollection2DrawStrategies(int[][] styleArrs, int size, int pxSize) {
//		IDrawStrategy[] result = new IDrawStrategy[size];
//		int n = styleArrs.length;
//		for (int i = 0; i < size; i++) {
//			result[i] = new StyledDrawStrategy(styleArrs[i % n], pxSize);
//		}
//		return result;
//	}
//
//	public static void styleInsteadColor(JEBExperimentControlKit expKit) {
//		int resourceCount = expKit.getSettingsContainer().getSettings().getResources().length;
//		IAdaptExperimentViewStrategy adaptExperimentViewStrategy = new AdaptExperimentViewStrategy(
//				DiagramUtils.styleArrayCollection2DrawStrategies(
//						STYLES_COLLECTION,
//						resourceCount,
//						10
//				),
//				null
//		);
//		expKit.setAdaptExperimentViewStrategy(adaptExperimentViewStrategy);
//	}

	public static double[] generateTimeStamps(BaseJEBExperimentSettings settings) {
		int stepCount = settings.stepCount;
		double[] result = new double[stepCount];
		for (int i = 0; i < stepCount; i++) {
			result[i] = i * settings.stepSize;
		}
		return result;
	}

	public static void substractPrev(double[] arr) {
		int n = arr.length;
		for (int i = n - 1; i > 0; i--) {
			arr[i] = arr[i] - arr[i - 1];
		}
	}

	public static DiagramData fillZeros(
			DiagramData d,
			Point zeroPoint
	) {
		int n = d.x.length;
		DiagramData fd = new DiagramData(n + 2);
		System.arraycopy(d.x, 0, fd.x, 1, n);
		System.arraycopy(d.y, 0, fd.y, 1, n);
		fd.x[0] = d.x[0];
		fd.y[n + 1] = d.y[n - 1];
		fd.x[n + 1] = zeroPoint.x;
		fd.y[0] = zeroPoint.y;
		return fd;
	}

//	public static void saveDiagrams(
//			ExperimentViewDrawer[] experimentViewDrawers,
//			IExperiment experiment,
//			String path,
//			int width,
//			int height
//	) {
//		int dotPos = path.lastIndexOf(".");
//		if (dotPos == -1) {
//			dotPos = path.length();
//		}
//		path = path.substring(0, dotPos);
//		int n = experimentViewDrawers.length;
//		for (int i = 0; i < n; i++) {
//			File f = new File(path + "_" + experiment.labelYAt(i) + ".jpg");
//			saveDiagram(experimentViewDrawers[i], f, width, height);
//		}
//	}
//
//	private static void saveDiagram(
//			ExperimentViewDrawer experimentViewDrawer,
//			File f,
//			int width, int height
//	) {
//		BufferedImage image = new BufferedImage(
//				width,
//				height,
//				BufferedImage.TYPE_INT_RGB
//		);
//		Graphics2D g = image.createGraphics();
//		g.setColor(Color.WHITE);
//		g.fillRect(0, 0, width, height);
//		g.setClip(0, 0, width, height);
//		g.setColor(Color.BLACK);
//		experimentViewDrawer.draw(g);
//		try {
//			ImageIO.write(image, "jpeg", f);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
}
